package com.lp.easymethodlink.ui;

import com.intellij.ui.components.JBScrollPane;
import com.lp.easymethodlink.model.CallHierarchyNode;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.*;

/**
 * 调用链路展示面板
 */
public class CallHierarchyPanel extends JPanel {
    
    private JTree tree;
    private DefaultTreeModel treeModel;
    private JTextArea detailArea;
    
    public CallHierarchyPanel() {
        initializeComponents();
        setupLayout();
    }
    
    /**
     * 初始化组件
     */
    private void initializeComponents() {
        // 创建树形结构
        DefaultMutableTreeNode root = new DefaultMutableTreeNode("调用链路");
        treeModel = new DefaultTreeModel(root);
        tree = new JTree(treeModel);
        tree.setRootVisible(true);
        tree.setShowsRootHandles(true);
        tree.setCellRenderer(new CallHierarchyTreeCellRenderer());
        
        // 创建详细信息区域
        detailArea = new JTextArea();
        detailArea.setEditable(false);
        detailArea.setLineWrap(true);
        detailArea.setWrapStyleWord(true);
        
        // 添加树节点选择监听器
        tree.addTreeSelectionListener(e -> {
            TreePath path = e.getPath();
            if (path != null) {
                DefaultMutableTreeNode node = (DefaultMutableTreeNode) path.getLastPathComponent();
                Object userObject = node.getUserObject();
                if (userObject instanceof CallHierarchyNode) {
                    showNodeDetails((CallHierarchyNode) userObject);
                }
            }
        });
    }
    
    /**
     * 设置布局
     */
    private void setupLayout() {
        setLayout(new BorderLayout());
        
        // 左侧树形结构
        JBScrollPane treeScrollPane = new JBScrollPane(tree);
        treeScrollPane.setPreferredSize(new Dimension(400, 300));
        
        // 右侧详细信息
        JBScrollPane detailScrollPane = new JBScrollPane(detailArea);
        detailScrollPane.setPreferredSize(new Dimension(300, 300));
        
        JSplitPane splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, treeScrollPane, detailScrollPane);
        splitPane.setDividerLocation(400);
        splitPane.setResizeWeight(0.7);
        
        add(splitPane, BorderLayout.CENTER);
    }
    
    /**
     * 显示调用链路
     * @param rootNode 根节点
     */
    public void displayCallHierarchy(CallHierarchyNode rootNode) {
        DefaultMutableTreeNode rootTreeNode = new DefaultMutableTreeNode(rootNode);
        buildTreeNodes(rootTreeNode, rootNode);
        treeModel.setRoot(rootTreeNode);
        tree.expandRow(0);
    }
    
    /**
     * 构建树节点
     * @param treeParent 树父节点
     * @param hierarchyNode 层次节点
     */
    private void buildTreeNodes(DefaultMutableTreeNode treeParent, CallHierarchyNode hierarchyNode) {
        for (CallHierarchyNode child : hierarchyNode.getChildren()) {
            DefaultMutableTreeNode childTreeNode = new DefaultMutableTreeNode(child);
            treeParent.add(childTreeNode);
            buildTreeNodes(childTreeNode, child);
        }
    }
    
    /**
     * 显示节点详细信息
     * @param node 节点
     */
    private void showNodeDetails(CallHierarchyNode node) {
        StringBuilder sb = new StringBuilder();
        sb.append("类名: ").append(node.getClassName()).append("\n");
        sb.append("方法名: ").append(node.getMethodName()).append("\n");
        
        if (node.getMethodDescription() != null && !node.getMethodDescription().isEmpty()) {
            sb.append("方法描述: ").append(node.getMethodDescription()).append("\n");
        }
        
        if (node.isControllerMethod()) {
            sb.append("API路径: ").append(node.getApiPath()).append("\n");
            sb.append("HTTP方法: ").append(node.getHttpMethod()).append("\n");
        }
        
        detailArea.setText(sb.toString());
        detailArea.setCaretPosition(0);
    }
}